package com.sniper.serviceribbon.hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandKey;
import com.sniper.model.LineShop;
import com.sniper.serviceribbon.service.LineShopService;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;

import static com.netflix.hystrix.HystrixCommandGroupKey.Factory.asKey;

/**
 * 单个数据读取合并
 *
 * @author suzhen
 * @create 2018/3/15
 */
public class LineShopBatchCommand extends HystrixCommand<List<LineShop>> {

    private LineShopService lineShopService;
    private List<String> ids;

    LineShopBatchCommand(LineShopService lineShopService, List<String> ids) {
        super(Setter.withGroupKey(asKey("lineShopServiceCommand"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("list")));
        this.lineShopService = lineShopService;
        this.ids = ids;
    }

    @Override
    protected List<LineShop> run() throws Exception {
        if (ids != null) {
            return lineShopService.list(StringUtils.join(ids, ","));
        }
        return Collections.emptyList();
    }
}
